import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import locale from 'element-ui/lib/locale/lang/en'
import moment from 'moment';
import cookie from "js-cookie";
import api from "@/api/api.js";


import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts


//vue生产提示的关闭
Vue.config.productionTip = false
Vue.prototype.$echarts = echarts
//处理日期
Vue.prototype.moment = moment;
Vue.use(ElementUI, { locale });


Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  router
}).$mount('#app')

router.beforeEach((to, from, next) => {

  // 1.如果访问的是登录页面（无需权限），直接放行
  if (to.path === '/login' || to.path === '/register') return next()
  // 2.如果访问的是有登录权限的页面，先要获取token
  const tokenStr = cookie.get('token')
  // 2.1如果token为空，强制跳转到登录页面；否则，直接放行
  if (tokenStr == "token") {
    return next('/login')
  }
  const id = cookie.get('id')
  //没有填写邮箱和手机号就不能跳转
  if(id){
    api
    .getUserInfo(id)
    .then((response) => {
      if (response.code == "200") {
        let userInfo = response.body;
        if(userInfo.email==''||userInfo.phone==''){
          return next(`/systemManagement/personalInformation/${userInfo.id}`)
        }
      }
    })
    .catch((error) => {
      console.log(error);
    });
  }
  next()
})